$(function () {
    $('#security-dg').datagrid({
        striped: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        scrollbarSize: 0,
        width: "auto",
        showFooter: true,
        url: 'security?method=findByPage',
        onBeforeSelect: function () {
            return false;
        },
        columns: [[
            {field: 'question', title: '安全问题', width: 20, align: 'left', halign: 'center'},
            {field: 'createdTime', title: '创建时间', width: 15, align: 'center', halign: 'center'},
            {field: 'updatedTime', title: '修改时间', width: 20, align: 'center'},
            {
                field: 'id', title: '操作', width: 15, formatter: function (value, row, index) {
                    let edit = '<button type="button" class="btn btn-primary btn-xs" name="edit" data-value="' + value + '" onclick="edit(this)"><i class="fa fa-pencil"></i>&nbsp;编辑</button>&nbsp;&nbsp;<button type="button" class="btn btn-danger btn-xs" name="del" data-value="' + value + '" onclick="del(' + value + ',\'' + row.question + '\')"><i class="fa fa-trash"></i>&nbsp;删除</button>';
                    return value === 0 ? "" : edit;
                }, align: 'center'
            },
        ]],
        toolbar: [
            {
                iconCls: 'icon-add',
                text: "新增",
                handler: function () {
                    $("#add").window('open');
                    $("#iconCls").focus();
                    $("#question").focus();
                }
            }
        ]
    });

    $("#add").window({
        width: 600,
        height: 200,
        title: '新增安全问题',
        iconCls: 'icon-add',
        collapsible: true,
        minimizable: false,
        maximizable: false,
        resizable: false,
        closed: true
    });

    $("#edit").window({
        width: 600,
        height: 200,
        title: '修改安全问题',
        iconCls: 'icon-save',
        collapsible: true,
        minimizable: false,
        maximizable: false,
        resizable: false,
        closed: true
    });

    $("#addQuestion").on("click", function () {
        $.post("security?method=save", $("#qaForm").serialize(), function (result) {
            if (result.status === 20000) {
                $("#add").window("close");
                $('#security-dg').datagrid('reload');
                $("#question").val("");
                layer.msg(result.msg, {icon: 1});
            } else {
                layer.msg(result.msg, {icon: 2});
            }
        }, "json");
    });

    $("#editQuestion").on("click", function () {
        $.post("security?method=save", $("#qaEditForm").serialize(), function (result) {
            if (result.status === 20000) {
                $("#edit").window("close");
                $('#security-dg').datagrid('reload');
                layer.msg(result.msg, {icon: 1});
            } else {
                layer.msg(result.msg, {icon: 2});
            }
        }, "json");
    });
});

function edit(obj) {
    let id = $(obj).attr("data-value");
    $.get("security", {"method": "findById", "id": id}, function (result) {
        $("#newQuestion").val(result.data.question);
        $("#qaId").val(result.data.id);
    }, "json");
    $("#edit").window('open');
    $("#newQuestion").focus();
}

function del(id, title) {
    layer.confirm("确认删除 <b style='color: red;'>" + title + "</b> 吗？", {title: "删除确认", icon: 3}, function (index) {
        $.get("security", {"method": "delete", "id": id}, function (result) {
            if (result.status === 20000){
                layer.msg(result.msg, {icon: 1});
            }else{
                layer.msg(result.msg, {icon: 2});
            }
            $('#security-dg').datagrid('reload');
        }, "json");
        layer.close(index);
    });
}